import Vue from "vue";
import VueRouter from "vue-router";
import { Toast } from "vant";

Vue.use(VueRouter);

const routes = [
    {   // 用户登录
        path: "/",
        name: "login",
        component: (resolve) => require(["@/views/LoginView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 显示信息页面
        path: "/message",
        name: "message",
        component: (resolve) => require(["@/views/MessageView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 好友界面
        path: "/friend",
        name: "friend",
        component: (resolve) => require(["@/views/FriendView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 添加好友界面
        path: "/addFriend",
        name: "addFriend",
        component: (resolve) => require(["@/views/AddFriendView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 同意好友申请页面
        path: "/newFriend",
        name: "newFriend",
        component: (resolve) => require(["@/views/NewFriendView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 聊天界面
        path: "/chat",
        name: "chat",
        component: (resolve) => require(["@/views/ChatView.vue"], resolve),
        meta: { keepAlive: true },
    },
    {   // 用户信息页面
        path: "/home",
        name: "home",
        component: (resolve) => require(["@/views/UserInfoView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 好友朋友圈页面
        path: "/user_dynamic_home",
        name: "UserDynamicHome",
        component: (resolve) => require(["@/views/UserDynamicHomeView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 我的圈子页面
        path: "/circle",
        name: "circle",
        component: (resolve) => require(["@/views/CircleView.vue"], resolve),
        meta: { keepAlive: true },
    },
    {   // 发布动态页面
        path: "/editDynamics",
        name: "editDynamics",
        component: (resolve) => require(["@/views/EditDynamicsView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 我的页面
        path: "/data",
        name: "data",
        component: (resolve) => require(["@/views/MyInfoView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 创建群聊页面
        path: "/groupChat",
        name: "groupChat",
        component: (resolve) => require(["@/views/CreateGroupChatView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 【动态】详情页
        path: "/dynamic_detail",
        name: "dynamicDetail",
        component: (resolve) => require(["@/views/DynamicDetailView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 改变昵称页面
        path: "/modify_nickname",
        name: "modifyNickname",
        component: (resolve) => require(["@/views/ModifyNicknameView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 修改封面
        path: "/modify_cover",
        name: "modifyCover",
        component: (resolve) => require(["@/views/ModifyCoverView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 修改头像
        path: "/modify_headImg",
        name: "modifyHeadImg",
        component: (resolve) => require(["@/views/ModifyHeadImgView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 更多操作页面
        path: "/moreOperate",
        name: "moreOperate",
        component: (resolve) => require(["@/views/MoreOperateView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 群聊设置页面
        path: "/groupMoreOperate",
        name: "groupMoreOperate",
        component: (resolve) => require(["@/views/GroupMoreOperateView.vue"], resolve),
        meta: { keepAlive: false },
    },
    {   // 群通知页面
        path: "/groupNotification",
        name: "groupNotification",
        component: (resolve) => require(["@/views/GroupNotificationView.vue"], resolve),
        meta: { keepAlive: false },
    },
];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

router.beforeEach((to, from, next) => { 
    if (document.cookie == '' && to.name != 'login') {
        Toast.fail('登录信息已过期，请重新登录');
        next({ name: "login" });
        location.reload();
    } else if (document.cookie != '' && to.name == 'login') {
        Toast.fail('您已登录');
        next({ name: "message" });
    } else {
        next();
    }
});

// 处理重复导航的报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => {
        if (err.name !== 'NavigationDuplicated') throw err;
    });
};

export default router;
